﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using PNProject.Core;
using PNProject.Core.Converter;
using PNProject.Models;

namespace PNProject.Controllers
{
    [Authorize]
    public class AdminAccountController : Controller
    {
         
        //
        // GET: /AdminAccount/

        public ActionResult Index()
        {
            var username = Membership.GetUser().UserName;
            var user = StaticData.xuserRepo.GetOne(a => a.Username == username);
            var role = Roles.GetRolesForUser()[0];
            var obj = XUserConverter.XUserTo(user);
            obj.Role = role;            
            return View(obj);
        }

        [HttpPost]
        public ActionResult Edit(XUserModel model, HttpPostedFileBase avatar)
        {
            Guid newid;
            try
            {
                newid = Guid.Parse(model.UserID);
            }
            catch (Exception e)
            {
                return RedirectToAction("Index");
            }
            var obj = XUserConverter.ToXUser(model);
            var dbo = StaticData.xuserRepo.GetOne(a => a.UserID == newid);
            if (dbo == null)
                return RedirectToAction("Index");
            bool legal = true;
            if (string.IsNullOrEmpty(model.Avatar))
            {
                if (avatar != null)
                {
                    legal = avatar.ContentType == "image/jpeg" || avatar.ContentType == "image/png" || avatar.ContentType == "image/gif" || avatar.ContentType == "image/jpg";
                    if (legal)
                    {
                        ServerHelper.DeleteFileOnServer(dbo.Avatar);
                        string time = PNLibraries.PNConvertToTimestamp(DateTime.Now).ToString();
                        string newName = time + "-" + avatar.FileName;
                        string path = "/Application_Data/Images/UserImages/";
                        ServerHelper.SaveFileBaseOnServer(path, avatar, newName);
                        obj.Avatar = path + newName;
                    }
                }
                else
                {
                    ServerHelper.DeleteFileOnServer(dbo.Avatar);
                }
            }

            if (dbo.Avatar != obj.Avatar)
                ServerHelper.DeleteFileOnServer(dbo.Avatar);
            obj.UserID = dbo.UserID;
            obj.Role = dbo.Role;
            obj.IsBlocked = dbo.IsBlocked;
            StaticData.xuserRepo.Save(dbo, obj);
            return RedirectToAction("Index");
        }

        [Authorize(Roles = "SuperAdmin")]
        public ActionResult CreateAccount()
        {
            return View(new RegisterModel());
        }

        [Authorize(Roles = "SuperAdmin")]
        public ActionResult ListUser()
        {
            var list = StaticData.xuserRepo.GetAll();
            List<XUserModel> result = new List<XUserModel>();
            foreach (var item in list)
            {
                var u = XUserConverter.XUserTo(item);
                result.Add(u);
            }
            return View(result);
        }

        [Authorize(Roles = "SuperAdmin")]
        public ActionResult ActiveAccount(string id)
        {
            var xuser = StaticData.xuserRepo.GetOne(a => a.Username == id);
            if (xuser != null)
            {
                var newobj = Core.Converter.XUserConverter.CopyXUser(xuser);
                newobj.IsBlocked = false;
                StaticData.xuserRepo.Save(xuser, newobj);
            }
            return RedirectToAction("Index");
        }

        [Authorize(Roles = "SuperAdmin")]
        public ActionResult BlockAccount(string id)
        {

            var xuser = StaticData.xuserRepo.GetOne(a => a.Username == id);
            if (xuser != null)
            {
                var newobj = Core.Converter.XUserConverter.CopyXUser(xuser);
                newobj.IsBlocked = true;
                StaticData.xuserRepo.Save(xuser, newobj);
            }
            return RedirectToAction("Index");
        }

    }
}
